package cn.ac.sec.liquid.dev_manage;

import javax.servlet.http.HttpSession;
import cn.ac.sec.liquid.common.model.Oilfield;
import cn.ac.sec.liquid.common.model.User;

import com.jfinal.core.Controller;

/**
 * 设备管理
 */
public class DevManageController extends Controller {

	/**
	 * 炼油厂
	 */
	public void index() {	
		
		HttpSession session = getSession(); //测试从session获取变量
		User user = (User)session.getAttribute("login_user");
		System.out.println("DevManageController.index() 当前登录用户： " + user.getUsername());
		session.setAttribute("current_user", user.getUsername());
		
		setAttr("factorytList", Oilfield.dao.find("select * from oilfield where layer_no=1"));
		render("/dev_manage/factory.html");
	}
	
	/**
	 * 工作组
	 */
	public void group() {
		setAttr("groupList", Oilfield.dao.find("select * from oilfield where layer_no=2"));
		render("/dev_manage/group.html");
	}
	
	/**
	 * 井场
	 */
	public void wellGroup() {
		setAttr("wellGroupList", Oilfield.dao.find("select * from oilfield where layer_no=3"));
		render("/dev_manage/wellGroup.html");
	}
	
	/**
	 * 油井
	 */
	public void well() {
		setAttr("wellList", Oilfield.dao.find("select * from oilfield where layer_no=4"));
		render("/dev_manage/well.html");
	}
	
	/**
	 * 添加设备
	 */
	public void add() {
		
	}
	
	/**
	 * 保存添加
	 * 同时通过redirect()调用对应的action，从数据重新加载数据进行显示
	 */
	public void save() {
		
		Oilfield oilfield = getModel(Oilfield.class);
		oilfield.save();
		redirect("/dev_manage"); 
	}
	
	/**
	 * 编辑
	 * getParaToInt():注入方式获取变量值，是主键值
	 * 流程： 在factory.html界面点击edit编辑，调用edit(),同时传入主键值，在edit()中获取主键，并根据主键获取对象，
	 *     以oilfield为名放入request域中
	 */
	public void edit() {
		System.out.println("edit(),所点击设备主键: " + getParaToInt());
		setAttr("oilfield" + "", Oilfield.dao.findById(getParaToInt())); //向edit.html界面传入的数据，在request域中
	}
	
	/**
	 * 更新显示
	 */
	public void update() {
		Oilfield oilfield = getModel(Oilfield.class);
		oilfield.update();
		redirect("/dev_manage");
	}
	
	/**
	 * 删除一项设备
	 */
	public void delete() {
		System.out.println("选择删除项主键： " + getParaToInt());
		Oilfield.dao.deleteById(getParaToInt());
		redirect("/dev_manage");
	}
	
	/**
	 * 功能：删除一组数据
	 * 注意：Oilfield.dao.deleteById()函数中必须使用主键primary_id,而不是id才可以
	 */
	public void batchDelete(){
		
		String ids[] = getParaValues("ids");
		for(String id : ids){
			Oilfield.dao.deleteById(Integer.valueOf(id));
			//Oilfield.dao.deleteById(31); //测试使用主键删除一行，成功
		}
		redirect("/dev_manage");
	}
}
